package org.easyframework.core.entity;

import java.io.Serializable;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("统一返回结果实体类")
public class ResponseResult<T> implements Serializable {

	private static final long serialVersionUID = -3630870436439258580L;
	public static final int SUCCESS = 200;
	public static final int FAILED = 500;

	@ApiModelProperty(value = "返回码: 200 处理成功， 500 处理失败", position = 1)
	private int code;

	@ApiModelProperty(value = "返回消息提示", position = 2)
	private String message;

	@ApiModelProperty(value = "返回数据", position = 3)
	private T data;

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public static <T> ResponseResult<T> getInst(int code, T data, String message) {
		ResponseResult<T> result = getInst(code, data);
		result.setMessage(message);
		return result;
	}

	public static <T> ResponseResult<T> getInst(int code, T data) {
		ResponseResult<T> result = getInst(code);
		result.setData(data);
		return result;
	}

	public static <T> ResponseResult<T> getInst(int code, String message) {
		ResponseResult<T> result = getInst(code);
		result.setMessage(message);
		return result;
	}

	public static <T> ResponseResult<T> getInst(int code) {
		ResponseResult<T> result = new ResponseResult<T>();
		result.setCode(code);
		return result;
	}

	public static <T> ResponseResult<T> getInst(int code, ProcessResult<T> presult) {
		return ResponseResult.getInst(code, presult.getData(), presult.getMessage());
	}

	public static <T> ResponseResult<T> getInst(ProcessResult<T> presult) {
		return ResponseResult.getInst(presult.isSuccess() ? ResponseResult.SUCCESS : ResponseResult.FAILED, presult.getData(), presult.getMessage());
	}

	public static <T> ResponseResult<T> getSuccess() {
		return getInst(ResponseResult.SUCCESS);
	}

	public static <T> ResponseResult<T> getFailed(String message) {
		return getInst(ResponseResult.FAILED, message);
	}

	@Override
	public String toString() {
		return "ResponseResult{" + "data=" + data + ", code=" + code + ", message='" + message + '\'' + '}';
	}

	public boolean isSuccess() {
		return code == SUCCESS;
	}

	public boolean isFailed() {
		return code == FAILED;
	}

	public static <T> ResponseResult<T> getFailedInst(String message) {
		ResponseResult<T> inst = getInst(FAILED);
		inst.setMessage(message);
		return inst;
	}

	public static <T> ResponseResult<T> getSuccess(String msg) {
		ResponseResult<T> success = getSuccess();
		success.setMessage(msg);
		return success;
	}

	public static <T> ResponseResult<T> getSuccess(T entity) {
		ResponseResult<T> result = getSuccess();
		result.setData(entity);
		return result;
	}
}
